package manageAbilitiesServlets;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.abilitiesSession.AbilitySessionRemote;

public class RemoveAbilityForUserServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		removeAbilityFromUser(request,response);
		try {
			response.sendRedirect("/ProgettoWeb/ManageAbilitiesForUser.html");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		removeAbilityFromUser(request,response);
		try {
			response.sendRedirect("/ProgettoWeb/ManageAbilitiesForUser.html");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private void removeAbilityFromUser(HttpServletRequest request, HttpServletResponse response) {
		try {
			Context jndiCtx = new InitialContext();
			AbilitySessionRemote requestAbilitySession = (AbilitySessionRemote) jndiCtx
					.lookup("Progetto/AbilitySession/remote");
			int idUser,idAbility;
			idAbility = Integer.parseInt((String)request.getParameter("nameAbility"));
			idUser=(Integer)request.getSession().getAttribute("id");
			if(requestAbilitySession.removeAbilityFromUser(idAbility,idUser))
				//abilita' rimossa correttamente
				request.getSession().setAttribute("result", "erased");
			else
				request.getSession().setAttribute("result", "error");
		} catch (NamingException e) {
			System.out.print("errore nella connessione remota");
			e.printStackTrace();
			request.getSession().setAttribute("result", "error");
		}
	}

}
